package com.a918.service.impl;

import com.a918.dao.*;
import com.a918.entity.*;
import com.a918.service.UserService;
import com.a918.util.PasswordHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImp implements UserService {

    private UserMapper userMapper;
    private UserlogMapper userlogMapper;

    @Autowired
    public UserServiceImp(UserMapper userMapper,  UserlogMapper userlogMapper) {
        this.userMapper = userMapper;
        this.userlogMapper = userlogMapper;
    }


    @Override
    public User getUserByUsername(String username) {
        return userMapper.getUserByUsername(username);
    }

    @Override
    public void insertUser(User user) {
        User userToDB = new PasswordHelper().encryptPassword(user);
        userMapper.insertSelective(userToDB);
    }

    @Override
    public boolean validatePasswd(User user, String oldpassword) {
        String jiamiPassword = new PasswordHelper().jiamiPasswd(oldpassword);
        return user.getPassword().equals(jiamiPassword);
    }

    @Override
    public void updateUser(User user) {
        User userToDB = new PasswordHelper().encryptPassword(user);
        userMapper.updateByPrimaryKey(userToDB);

    }


    @Override
    public User getUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }


    @Override
    public void insertlog(String des, String url, String username,String ip) {
            Userlog log = new Userlog();
            log.setUrl(url);
            log.setDes(des);
            log.setUsername(username);
            log.setIp(ip);
            userlogMapper.insertSelective(log);
    }

    @Override
    public List<Userlog> getLogList(Map<String, String> paramMap) {
        return userlogMapper.getLogList(paramMap);
    }


    @Override
    public void updateCheckstatus(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public void updateUserCheckstatus(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }


}
